package com.atguigu.team.service;
import com.atguigu.team.domain.*;

/**
 * @Author 代码渌
 * @Date  20:14
 * @Description 负责将Data中的数据封装到Employee[]数组中，同时提供相关操作Employee[]的方法
 * @Since version-1.0
 */
public class NameListService {
    private Employee[] employees;

    /**
     * 给数组及数组原始初始化
     */
    public NameListService(){
        //根据项目提供的Data类构建相应大小的employee数组
        //再根据Data类中的数据构建不同的对象，包括Employee、Programmer、Designer和Architect
        //将对象存储于数组中
        employees = new Employee[Data.EMPLOYEES.length];
        for (int i = 0;i<employees.length;i++){
            //获取员工id
            int type = Integer.parseInt(Data.EMPLOYEES[i][0]);

            //获取Employee的4个基本类型
            int id = Integer.parseInt(Data.EMPLOYEES[i][1]);
            String  name = Data.EMPLOYEES[i][2];
            int age = Integer.parseInt(Data.EMPLOYEES[i][3]);
            double salary = Double.parseDouble(Data.EMPLOYEES[i][4]);
            Equipment equipment;
            double bonus;
            int stock;
            switch (type){
                case Data.EMPLOYEE:
                    employees[i] = new Employee(id,name,age,salary);
                    break;
                case Data.PROGRAMMER:
                    equipment = createEquipmet(i);
                    employees[i] = new Programmer(id,name,age,salary,equipment);
                    break;
                case Data.DESIGNER:
                    equipment = createEquipmet(i);
                    bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
                    employees[i] = new Designer(id,name,age,salary,equipment,bonus);
                    break;
                case Data.ARCHITECT:
                    equipment = createEquipmet(i);
                    bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
                    stock =Integer.parseInt(Data.EMPLOYEES[i][6]);
                    employees[i] = new Architect(id,name,age,salary,equipment,bonus,stock);
                    break;
            }
        }

    }
    //获取指定index上员工的设备
    private Equipment createEquipmet(int index) {
        int key = Integer.parseInt(Data.EQUIPMENTS[index][0]);
        String modelOrName = Data.EQUIPMENTS[index][1];
        switch (key){
            case Data.PC:   //21
                String display = Data.EQUIPMENTS[index][2];
                return new PC(modelOrName,display);
            case Data.NOTEBOOK: //22
                double price = Double.parseDouble(Data.EQUIPMENTS[index][2]);
                return new NoteBook(modelOrName,price);
            case Data.PRINTER:  //23
                String type = Data.EQUIPMENTS[index][2];
                return new Printer(modelOrName,type);
        }
        return null;
    }

    //获取当前所有员工
    public Employee[] getAllEmployees(){
        return employees;
    }
    //获取指定id的员工
    public Employee getEmployees(int id) throws TeamException {
        for (int i = 0;i<employees.length;i++){
            if (employees[i].getId() == id){
                return  employees[i];
            }
        }
        throw new TeamException("找不到指定的员工");
    }
}
